/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.scale
{

import blocstac.api.blocbox.MimeType;
import blocstac.editimage.base.ImageModel;
import blocstac.editimage.base.ImageOperation;

import flash.display.BitmapData;
import flash.geom.Point;

import flexunit.framework.TestCase;

public class ScaleOperationTest extends TestCase
{
	
    //==========================================================================
    //
    //  Test set up and tear down
    //
    //--------------------------------------------------------------------------  
	private var op:ImageOperation
	private var imageModel:ImageModel
	private var mouseLocationInUnscaledImageComp:Point
	private var scaleInit:ScaleInit
	private var imageData:BitmapData
	
	override public function setUp(): void
	{
		scaleInit = new ScaleInit()
		scaleInit.editedScaleX = 17
		scaleInit.editedScaleY = 19
		scaleInit.brImageCornerInRotationCompUnscaled = new Point(21, 23)
		scaleInit.mouseToImageCornerOffsetUnscaled = new Point(27, 29)
		
		mouseLocationInUnscaledImageComp = new Point(20, 20)
		op = new ScaleOperation(scaleInit, mouseLocationInUnscaledImageComp, true)
		
		imageData = new BitmapData(9, 13)
		
		imageModel = new ImageModel()
		imageModel.angle = 11
		imageModel.editedScaleX = 23
		imageModel.editedScaleY = 57
		imageModel.mimeType = new MimeType(MimeType.MIMETYPE_PNG)
		imageModel.updateImageData(imageData)
		imageModel.resetImageDataChanged()
	}
	
	override public function tearDown():void
	{
	}
	
    //==========================================================================
    //
    //  Tests
    //
    //--------------------------------------------------------------------------  
	[Test]
	public function test_apply(): void
	{
		op.apply(imageModel)

		assertEquals(11, imageModel.angle)
		assertEquals(13.654462242562929, imageModel.editedScaleX)
		assertEquals(15.26086956521739, imageModel.editedScaleY)
		
		assertTrue(imageData === imageModel.imageData)
		assertFalse(imageModel.imageDataChanged)
	}

	[Test]
	public function test_apply_dont_keep_ratio(): void
	{
		op = new ScaleOperation(scaleInit, mouseLocationInUnscaledImageComp, false)
		op.apply(imageModel)

		assertEquals(11, imageModel.angle)
		assertEquals(14.904761904761905, imageModel.editedScaleX)
		assertEquals(15.26086956521739, imageModel.editedScaleY)
		
		assertTrue(imageData === imageModel.imageData)
		assertFalse(imageModel.imageDataChanged)
	}
	
	[Test]
	public function test_prevent_applying_negative_scales(): void
	{
		mouseLocationInUnscaledImageComp = new Point(-10, -10)
		op = new ScaleOperation(scaleInit, mouseLocationInUnscaledImageComp, true)
		op.apply(imageModel)

		assertEquals(11, imageModel.angle)
		assertEquals(23, imageModel.editedScaleX)
		assertEquals(57, imageModel.editedScaleY)
		
		assertTrue(imageData === imageModel.imageData)
		assertFalse(imageModel.imageDataChanged)
	}	

	[Test]
	public function test_undo(): void
	{
		op.apply(imageModel)
		op.undo(imageModel)
		
		assertEquals(11, imageModel.angle)
		assertEquals(17, imageModel.editedScaleX)
		assertEquals(19, imageModel.editedScaleY)
		
		assertTrue(imageData === imageModel.imageData)
		assertFalse(imageModel.imageDataChanged)
	}

}
}